package com.example.myapplication.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.EncodeUtils
import com.example.myapplication.base.BaseActivity
import com.example.myapplication.databinding.LoginActivityBinding
import com.example.myapplication.http.api.LoginApi
import com.example.myapplication.http.model.HttpData
import com.google.gson.Gson
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import okhttp3.Call
import org.json.JSONObject

/**
 *    author :
 *    github : https://github.com/getActivity/AndroidProject-Kotlin
 *    time   : 2018/10/18
 *    desc   : 登录界面
 */
class LoginActivity : BaseActivity<LoginActivityBinding>(){

    /***静态类*/
    companion object {

        private const val INTENT_KEY_IN_PHONE: String = "number"
        private const val INTENT_KEY_IN_PASSWORD: String = "password"

        fun start(context: Context, number: String?, password: String?) {
            val intent = Intent(context, LoginActivity::class.java)
            intent.putExtra(INTENT_KEY_IN_PHONE, number)
            intent.putExtra(INTENT_KEY_IN_PASSWORD, password)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }


    override fun initView() {

    }

    override fun initData() {
        binding.btnLoginCommit.setOnClickListener {
            if (binding.etLoginPhone.text.toString() == "") {
                toast("请输入工号")
                return@setOnClickListener
            }
            if (binding.etLoginPassword.text.toString() == "") {
                toast("请输入密码")
                return@setOnClickListener
            }

            // 隐藏软键盘
            hideKeyboard(currentFocus)
            var map = HashMap<String, String>()
            map["uName"] = binding.etLoginPhone.text.toString()
            map["uPwd"] = binding.etLoginPassword.text.toString()
            map["uServerIdx"] = "0"
            val json = JSONObject(map.toString())
            android.util.Log.e("sss", json.toString())
            android.util.Log.e("sss1", Gson().toJson(map.toString()))

            var data = EncodeUtils.urlEncode(json.toString(), "UTF-8")
            android.util.Log.e("sss2", data)

            map["data"] =
                "%257B%2522uName%2522%253A%2522%25E7%25A6%258F%25E5%25B7%259E%2522%252C%2522uServerIdx%2522%253A%25220%2522%252C%2522uPwd%2522%253A%2522123%2522%257D"
            map["action"] = "LOGIN"
            android.util.Log.e("sss3", map.toString())
            EasyHttp.post(this)
                .api(LoginApi().apply {
                    setMap(map)
                })
                .request(object : HttpCallback<HttpData<LoginApi.Bean>>(this) {

                    override fun onStart(call: Call) {
                    }

                    override fun onEnd(call: Call) {}

                    override fun onSucceed(data: HttpData<LoginApi.Bean>) {
                        startActivity(MainActivity::class.java)
                        finish()
                    }

                    override fun onFail(e: Exception?) {
                        super.onFail(e)
                    }
                })
        }
    }


    override fun initLayout() {

    }
}